package com.vercon.common.network;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.vercon.common.BuildConfig;
import com.vercon.common.Constants;

import java.net.Proxy;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
/**
 * 网络请求
 */

public class MainApi {
    private static MainApi mainApi;

    public static MainApi getInstance() {
        if (mainApi == null) {
            synchronized (MainApi.class) {
                if (mainApi == null) {
                    mainApi = new MainApi();
                }
            }
        }
        return mainApi;
    }

    public <T> T getApi(Class<T> api) {
        return createApi("", api);//默认地址使用此方法
    }

    /**
     * 入不同的baseURL
     *
     * @param url
     * @return
     */
    public <T> T getApi(String url, Class<T> api) {
        return createApi(url, api);
    }

    private <T> T createApi(String url, Class<T> api) {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(15, TimeUnit.SECONDS);
        builder.writeTimeout(60, TimeUnit.SECONDS);
        builder.readTimeout(60, TimeUnit.SECONDS);
        builder.retryOnConnectionFailure(false);//超时重试
        if (!BuildConfig.DEBUG) {
            builder.proxy(Proxy.NO_PROXY);
        }
        if (BuildConfig.DEBUG) {
            //debug才打印日志
            builder.addInterceptor(interceptor);
        }
        builder.addInterceptor(new ReadCookiesInterceptor());
        OkHttpClient okHttpClient = builder
                //忽略https的证书校验，否则报Trust anchor for certification path not found.
                .sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(X509Certificate[] chain, String authType) {

                    }

                    @Override
                    public void checkServerTrusted(X509Certificate[] chain, String authType) {

                    }

                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[0];
                    }
                })
                .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
                .build();

        if (TextUtils.isEmpty(url)) {
            if (BuildConfig.DEBUG) {
                //测试版
                url = Constants.BASE_DEBUG_URL;//默认地址
               // url = Constants.BASE_RELEASE_URL;
            } else {
                //url = Constants.PRE_RELEASE_DEBUG_URL;//预发布地址

                //正式版
                url = Constants.BASE_RELEASE_URL;
            }
        }

        Retrofit retrofit = new Retrofit.Builder()
                //设置服务器路径
                .baseUrl(url)
                //设置使用okhttp网络请求
                .client(okHttpClient)
                //添加转化库，默认是Gson
                .addConverterFactory(GsonConverterFactory.create(new Gson()))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return retrofit.create(api);
    }
}
